package com.train.controller;

import com.train.dto.AttDTO;
import com.train.dto.CourseDTO;
import com.train.pojo.Card;
import com.train.pojo.Course;
import com.train.pojo.Department;
import com.train.service.AttService;
import com.train.service.CardService;
import com.train.service.CourseService;
import com.train.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Collection;
import java.util.List;

@Controller
public class CourseController {
    @Autowired
    private CardService cardService;

    @Autowired
    private CourseService couseService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private AttService attService;

    @PostMapping("/findCourse")
    public String checkin(@RequestParam(value = "coursename",required = false)String coursename,
                          @RequestParam(value = "place",required = false)String place,
                          @RequestParam(value = "departmentname",required = false)String departmentname,
                          @RequestParam(value = "credits",required = false)Integer credits,
                          @RequestParam(value = "type",required = false)Integer type,
                          Model model){
        CourseDTO courseDTO=new CourseDTO();
        courseDTO.setCoursename(coursename);
        courseDTO.setPlace(place);
        courseDTO.setDepartmentname(departmentname);
        courseDTO.setCredits(credits);
        courseDTO.setType(type);

        Collection<CourseDTO> courseDTOS=couseService.findByRequired(courseDTO);
        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("courses",courseDTOS);
        model.addAttribute("departments",departments);
        return "train.html";
    }

    @GetMapping("/course")
    public String list(@ModelAttribute("msg")String msg, @ModelAttribute("msg")String msgCode,Model model){
        Collection<CourseDTO> courses = couseService.selectAllCourseDTO();
//        将结果放在请求中
        model.addAttribute("courses",courses);
        return "course/courseList.html";
    }

    @GetMapping("/addCourse")
    public String toAdd(Model model){
        //查出所有的部门，提供选择
        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("departments",departments);
        return "course/addCourse.html";
    }
    @PostMapping("/addCourse")
    public String add(Course course, RedirectAttributes rettr){
        rettr.addFlashAttribute("" +
                "msg","课程添加成功");
        rettr.addFlashAttribute("msgCode","200");
        //保存员工信息
        couseService.addCourse(course);
        //回到员工列表页面，可以使用redirect或者forward
        return "redirect:/course";
    }

    @GetMapping("/course/{id}")
    public String toUpdateCourse(@PathVariable("id") Integer id, Model model){
        //根据id查出来员工
        Course course = couseService.selectCourseById(id);
        //将员工信息返回页面
        model.addAttribute("course",course);
        //查出所有的部门，提供修改选择
        Collection<Department> departments = departmentService.selectAllDepartment();
        model.addAttribute("departments",departments);

        return "course/updateCourse.html";
    }

    @PostMapping("/updateCourse")
    public String updateCourse(Course course, RedirectAttributes rettr){
        rettr.addFlashAttribute("" +
                "msg","课程修改成功");
        rettr.addFlashAttribute("msgCode","200");
        couseService.updateCourse(course);
        //回到员工列表页面
        return "redirect:/course";
    }

    @GetMapping("/delCourse/{id}")
    public String deleteCourse(@PathVariable("id")Integer id,RedirectAttributes rettr){
        Collection<AttDTO> attDTOS=attService.selectAttByCouresId(id);
        if (attDTOS.isEmpty()){
            rettr.addFlashAttribute("" +
                    "msg","课程删除成功");
            rettr.addFlashAttribute("msgCode","200");
            //根据id删除员工
            couseService.deleteCourse(id);
            return "redirect:/course";
        }
        else {
            rettr.addFlashAttribute("" +
                    "msg","课程删除失败，该课程存在关联考勤记录");
            rettr.addFlashAttribute("msgCode","400");
            //根据id删除员工
            return "redirect:/course";
        }

    }

    @GetMapping("/findcardid")
    @ResponseBody
    public Card findcardid(Model model,String str){
        System.out.println("==============="+str);
        Card card=cardService.selectCardByStr(str);
        return  card;
    }

    @GetMapping("/getCourseBydepid")
    @ResponseBody
    public List<Course> getCourseBydepid(Integer departmentid){
        System.out.println("________________________"+departmentid);
        return couseService.getCourseBydepid(departmentid);
    }
}
